package com.etc.traval.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.etc.traval.biz.PlaceBiz;
import com.etc.traval.biz.PlacecomBiz;
import com.etc.traval.biz.UserBiz;
import com.etc.traval.bizimpl.PlaceBizImpl;
import com.etc.traval.bizimpl.PlacecomBizImpl;
import com.etc.traval.bizimpl.UserBizImpl;
import com.etc.travel.entity.Place;
import com.etc.travel.entity.Placecom;
import com.etc.travel.entity.User;
import com.google.gson.Gson;

public class PlacecomServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public PlacecomServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		// 获取传入的flag值
		PlacecomBiz biz = new PlacecomBizImpl();
		String flag = null;
		if (request.getParameter("flag") != null) {
			flag = request.getParameter("flag");
		}
		if (flag.equals("more")) {
			int id = Integer.valueOf(request.getParameter("op"));
			PlaceBiz biz1 = new PlaceBizImpl();
			Place place = biz1.queryPlaceById(id);
			int size = 5; // 每页显示5条数据
			int count = biz.queryCountByPlace(place); // 总条数
			int pageCount = (count % size == 0) ? (count / size) : (count / size + 1); // 总页数
			// 假定地址栏在每次分页查询的过程中都会传递一个key为page的值，如果是第一次进来该处理页，这该key对应的值不存在
			int pageIndex = 1;// 默认进来都是首页
			String addpage = request.getParameter("pageIndex");
			if (addpage == null) {
				pageIndex = 1;
			} else {
				// 如果地址栏上有确定是第几页，那么以地址栏对应的页数为准
				pageIndex = Integer.valueOf(addpage);
				if (pageIndex <= 0) {
					pageIndex = 1;
				} else if (pageIndex > pageCount) {
					pageIndex = pageCount;
				}
			}
			// 设置上一页，下一页
			int lastindex = pageIndex - 1 > 0 ? (pageIndex - 1) : 1; // 上一页容错
			int nextindex = pageIndex + 1 > pageCount ? pageCount : (pageIndex + 1); // 下一页容错
			// 查询得到对应条数和页数的数据
			List<Placecom> list = biz.queryPlacecomByPlace(place, pageIndex, size);
			// 将list数据放入请求域
			request.setAttribute("list", list);
			request.setAttribute("place", place);
			// 将总页数设置到请求域中
			request.setAttribute("pageCount", pageCount);
			// 将总条数设置到请求域中
			request.setAttribute("count", count);
			// 将上一页，下一页的值，存入请求域中
			request.setAttribute("lastindex", lastindex);
			request.setAttribute("nextindex", nextindex);
			// 将当前页数传递到请求域中
			request.setAttribute("pageIndex", pageIndex);
			// 采用转发，将登录的用户转发到newsManager.jsp
			request.getRequestDispatcher("yesplace.jsp").forward(request, response);
		}else if(flag.equals("add")){
			UserBiz biz1=new UserBizImpl();
			PlaceBiz biz2=new PlaceBizImpl();
			String content= request.getParameter("content");
			int userid=Integer.valueOf(request.getParameter("user"));
			int placeid= Integer.valueOf(request.getParameter("place"));
			User user=biz1.queryUserById(userid);
			Place place=biz2.queryPlaceById(placeid);
			Date date=new Date();
			Timestamp date1 =new Timestamp(date.getTime());
			Placecom placecom=new Placecom();
			placecom.setPlacecom_content(content);
			placecom.setPlacecom_place(place);
			placecom.setPlacecom_time(date1);
			placecom.setPlacecom_user(user);
			if(biz.addPlacecom(placecom)){
				request.getRequestDispatcher("PlaceServlet?flag=query&op="+placeid).forward(request, response);
			}else{
				
			}
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
